package com.lepu.app.fun.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.ObserverConst;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.ObserverManager;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.lepu.app.fun.my.bean.BeanQuestionAnswer;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class MyConsultDetailActivity extends BaseFragmentActivity
		implements OnTopbarNewLeftLayoutListener,AsyncRequest{

	private static final String REQUEST_ADD_HELP = "request_add_help";
	
	private CustomTopBarNew mTopbar;
	private BeanQuestionAnswer mBeanQuestionAnswer;

	private boolean mCurrentOpeHelp = false;
	private TextView mHelpStateTextView = null;
	private  int mPosition = 0;

	private boolean mGetData = true;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_consult_detail_activity);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		mTopbar = (CustomTopBarNew) findViewById(R.id.topbar);
		mTopbar.setTopbarTitle("问题详情");
		mTopbar.setonTopbarNewLeftLayoutListener(this);

		mHelpStateTextView = (TextView) findViewById(R.id.helpStateTextView);

		Bundle data = getIntent().getExtras();
		if (data != null && data.size() > 0) {
			mBeanQuestionAnswer = (BeanQuestionAnswer)data.getSerializable("BeanQuestionAnswer");
			mPosition = data.getInt("Position");

			TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
			TextView detailTextView = (TextView) findViewById(R.id.detailTextView);
			TextView readTextView = (TextView) findViewById(R.id.readTextView);
			TextView helpTextView = (TextView) findViewById(R.id.helpTextView);

			titleTextView.setText(mBeanQuestionAnswer.ARQuestion);
			detailTextView.setText(mBeanQuestionAnswer.ARAnswer);
			readTextView.setText(" 阅读  " + mBeanQuestionAnswer.ViewCount + "");
			helpTextView.setText(" 点赞  " + mBeanQuestionAnswer.FavourCount + "");

			updateFavor("0");

			if(LoginUtil.checkIsLogin())
			{
				requestAddHelp(1);
			}

		}
	}

	private void updateViewCount() {
		TextView readTextView = (TextView) findViewById(R.id.readTextView);
		int viewCount = UtilityBase.parseInt(mBeanQuestionAnswer.ViewCount) + 1;
		readTextView.setText(" 阅读  " + viewCount);

		MyConsultActivity myConsultActivity = (MyConsultActivity) AppManager.getAppManager().getActivity(MyConsultActivity.class);
		if(myConsultActivity != null)
		{
			myConsultActivity.updateItemRead(mPosition, viewCount);
		}

		mBeanQuestionAnswer.ViewCount = String.valueOf(viewCount);
		ObserverManager.getInstance().notify(ObserverConst.NOTIFY_INDEX_UPDATE_QUESTION,mPosition,mBeanQuestionAnswer);
	}

	private void updateFavor(String state) {
		if (!TextUtils.isEmpty(state) && state.equals("1")) {
			mHelpStateTextView.setSelected(true);
			mHelpStateTextView.setText("已点赞");
			
			UIHelper.showToast(this, "点赞成功");

			TextView helpTextView = (TextView) findViewById(R.id.helpTextView);
			int helpCount = UtilityBase.parseInt(mBeanQuestionAnswer.FavourCount) + 1;
			helpTextView.setText(" 点赞  "+helpCount);

			MyConsultActivity myConsultActivity = (MyConsultActivity) AppManager.getAppManager().getActivity(MyConsultActivity.class);
			if(myConsultActivity != null)
			{
				myConsultActivity.updateItemHelp(mPosition, helpCount);
			}

			mBeanQuestionAnswer.FavourCount = String.valueOf(helpCount);
			ObserverManager.getInstance().notify(ObserverConst.NOTIFY_INDEX_UPDATE_QUESTION,mPosition,mBeanQuestionAnswer);
		} else {
			mHelpStateTextView.setSelected(false);
			mHelpStateTextView.setText("点赞");
		}
	}

	public void viewClick(View view) {
		int rid = view.getId();
		switch (rid) {

			case R.id.helpStateTextView:

				boolean flag = LoginUtil.jumpLogin(this);

				if (flag) {
					return;
				}

				if(mCurrentOpeHelp)
				{
					return;
				}

				mCurrentOpeHelp = true;

				boolean selected = mHelpStateTextView.isSelected();
				if(!selected)
				{
					requestAddHelp(2);
				}
				
				break;
			default:
				break;
		}
	}

	/**
	 * add help请求
	 */
	private void requestAddHelp(int type) {
		String url = Setting.getUserQuestionHelpAddUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID",LoginUtil.getUserId());
			params.put("LoginToken",LoginUtil.getLoginToken());

			params.put("ArticleID", mBeanQuestionAnswer.ARQAID);

			//1：只增阅读数 2：增加点赞数和阅读数
			params.put("ActionType", type);

		} catch (Exception e) {
		}
		map.put("ht", params);

		if(type == 1)
		{
			showProgressDialog();
		}
		ApiClient.http_post_main(url, map, null, this, REQUEST_ADD_HELP, true);
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_ADD_HELP)) {
			hideProgressDialog();

			if(mGetData)
			{
				mGetData = false;
				updateViewCount();
			}else{
				mCurrentOpeHelp = false;
				updateFavor("1");
			}
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage)  {
		if (object.equals(REQUEST_ADD_HELP)) {
			hideProgressDialog();
			
			mGetData = false;
			
			if(mGetData)
			{
				mGetData = false;
			}else{
				mCurrentOpeHelp = false;
			}
		}
	}

}
